package com.example.speedwaymanager;

import android.annotation.SuppressLint;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Locale;
import java.util.Random;

@SuppressLint("DefaultLocale")
public class CTeam {

    protected ArrayList<CRider> _riders = new ArrayList<CRider>();
    protected ArrayList<CRider> _seniorRiders = new ArrayList<CRider>();
    protected ArrayList<CRider> _juniorRiders = new ArrayList<CRider>();

    protected Integer _points;
    protected String _name;
    protected ArrayList<CRider> _squad = new ArrayList<CRider>();
    protected CCoach _coach;
    protected boolean _isUser = false;

    public CTeam() {

    }

    public CTeam(CTeam team) {
        _riders = new ArrayList<CRider>(team._riders);
        _seniorRiders = new ArrayList<CRider>(team._seniorRiders);
        _juniorRiders = new ArrayList<CRider>(team._juniorRiders);
        _squad = new ArrayList<CRider>(team._squad);
        _name = team._name;
        _coach = team._coach;
        _isUser = team._isUser;
        _points = team._points;
    }

    /**
     * constructs a team with name REMARK: to add riders call addRiderToTeam()
	 *
     * @param name name of a team
     */
	public CTeam(String name) {
		_name = name;
        _coach = new CCoach("Coach", this);
    }

	@Override
	public String toString()
	{
		return _name;
	}
	
	public void addRiderToTeam(CRider rider) {
		_riders.add(rider);
		if (rider.get_type() == ERiderType.eJunior) {
			_juniorRiders.add(rider);
		} else {
			_seniorRiders.add(rider);
		}
	}

    /**
     * generates match squad should be overridden
     */
    public void createSquad() {

    }

    protected void squad() {
        Random generator = new Random(System.currentTimeMillis());
        Collections.reverse(_seniorRiders);
        Collections.reverse(_juniorRiders);

        ArrayList<CRider> _seniors123 = new ArrayList<CRider>();
        _seniors123.add(_seniorRiders.get(0));
        _seniors123.add(_seniorRiders.get(1));
        _seniors123.add(_seniorRiders.get(2));

        ArrayList<CRider> _seniors45 = new ArrayList<CRider>();
        _seniors45.add(_seniorRiders.get(3));
        _seniors45.add(_seniorRiders.get(4));

        ArrayList<CRider> _juniors67 = new ArrayList<CRider>();
        _juniors67.add(_juniorRiders.get(0));
        _juniors67.add(_juniorRiders.get(1));

        Collections.shuffle(_seniors123, generator);
        Collections.shuffle(_seniors45, generator);
        Collections.shuffle(_juniors67, generator);

        _squad.clear();
        _squad.add(_seniors123.get(0));
        _squad.add(_seniors45.get(0));
        _squad.add(_seniors123.get(1));
        _squad.add(_seniors45.get(1));
        _squad.add(_seniors123.get(2));
        _squad.add(_juniors67.get(0));
        _squad.add(_juniors67.get(1));
    }

    public void clearAllMatchPoints() {
        for (CRider rider : _riders) {
			rider.clearPoints();
		}
		_points = 0;
	}

	public int calculateTeamPoints() {
		_points = 0;
		for (CRider rider : _squad) {
			_points += rider.get_matchStats().get_points();
		}
		return _points;
	}
	
	/**
	 * retrieve rider with given number during match
     * @return returns rider with given number //TODO could it be a map?
     */
	public CRider getRiderWithNumber(int number)
	{
		CRider rider = null;
		for (CRider r:_squad) {		
			if ( r.get_number() == number)
			{
				rider = r;
			}
		}
		return rider;
	}

	/**
	 * @return the _name
	 */
	public String get_name() {
		return _name;
	}
	
	@SuppressLint("NewApi")
	public static String decompose(String s) {
	    return java.text.Normalizer.normalize(s, java.text.Normalizer.Form.NFD).replaceAll("\\p{InCombiningDiacriticalMarks}+","");
	}
	
	/**
	 * @return the resource id name without polish signs and spaces
	 */
	@SuppressLint("DefaultLocale")
	public String get_resourceName() {
		String lower = _name.toLowerCase(Locale.ENGLISH);
		lower = lower.substring(lower.lastIndexOf(" ") + 1, lower.length());
		//lower = lower.replaceAll(" ", "");
		lower = decompose(lower);
		return lower;
	}

	/**
	 * @return the _riders
	 */
	public ArrayList<CRider> get_riders() {
		return _riders;
	}

	/**
	 * @return the _points
	 */
	public int get_points() {
		return _points;
	}

	/**
	 * @return the _squad
	 */
	public ArrayList<CRider> get_squad() {
		return _squad;
	}

	/**
	 * @return the _homeTeam
	 */
    public Boolean is_HomeTeam() {
        return null;
    }


    /**
     * @return the _coach
	 */
	public CCoach get_coach() {
		return _coach;
	}

	/**
	 * @return the _seniorRiders
	 */
	public ArrayList<CRider> get_seniorRiders() {
		return _seniorRiders;
	}

	/**
	 * @return the _juniorRiders
	 */
	public ArrayList<CRider> get_juniorRiders() {
		return _juniorRiders;
	}

	/**
	 * @return the _isUser
	 */
	public boolean is_isUser() {
		return _isUser;
	}

	/**
	 * @param _isUser the _isUser to set
	 */
	public void set_isUser(boolean _isUser) {
		this._isUser = _isUser;
	}

	/**
	 * @param _squad the _squad to set
	 */
	public void set_squad(ArrayList<CRider> _squad) {
		this._squad = _squad;
	}
}
